<?php

namespace app\controller\admin;

use app\base\BaseEncryptController;
use app\model\distributor\DistributorModel;
use think\Request;

/**
 * User
 * Class User
 * @package app\controller
 */
class Distributor extends BaseEncryptController
{
    protected function initialize()
    {
        parent::initialize();
        $this->model = new DistributorModel();
    }

    /**
     * 分页列表
     * @param string $status
     * @param string $keyword
     * @param int $page
     * @param int $limit
     * @return \think\response\Json
     */
    public function getPage($status = '', $keyword = '', $page = 1, $limit = 10)
    {
        $map = [];
        if (!empty($status)) {
            $map['status'] = $status;
        }
        if (!empty($keyword)) {
            $map['like'] = [
                'name' => "%" . $keyword . "%",
                'phone' => "%" . $keyword . "%",
            ];
        }
        $data = $this->model->getPageList($map, 'id DESC', $page, $limit);
        return self::returnMsg(1, [
            'totalCount' => $data['total'],
            'currentPage' => $data['current'],
            'pageList' => $data['dataList']
        ]);
    }

    /**
     * 获取全部渠道商
     * @return \think\response\Json
     */
    public function getAll()
    {
        $data = $this->model->getAll([],"id,name");
        if (!empty($data)) {
            return self::returnMsg(1, $data);
        } else {
            return self::returnMsg(1001);
        }
    }

    /**
     * 单条信息
     * @param $id
     * @return \think\response\Json
     */
    public function getInfo($id)
    {
        $data = $this->model->getInfo($id);
        if (!empty($data)) {
            return self::returnMsg(1, $data);
        } else {
            return self::returnMsg(1001);
        }
    }


    /**
     * 保存
     * @param Request $request
     * @return \think\response\Json
     */
    public function onSave(Request $request)
    {
        $formData = [
            'id' => $request->param('id'),
            'name' => $request->param('name'),
            'phone' => $request->param('phone'),
            'business' => $request->param('business'),
            'address' => $request->param('address'),
            'comp_url' => $request->param('comp_url'),
            'status' => $request->param('status'),
            'remark' => $request->param('remark')
        ];
        if (empty($formData['id'])) {
            $validate = 'app\validate\DistributorValidate.add';
        } else {
            $validate = 'app\validate\DistributorValidate';
        }
        $result = $this->validate($formData, $validate);

        if (true !== $result) {
            return self::returnMsg(991, [], $result);
        } else {
            $ret = $this->model->saveData($formData, true, true);
            if (false !== $ret['flag']) {
                return self::returnMsg(1);
            } else {
                return self::returnMsg(0);
            }
        }
    }

    /**
     * 删除
     * @param $id
     * @return \think\response\Json
     */
    public function onDelete($id)
    {
        $flag = $this->model::destroy(['id' => $id]);
        if (false !== $flag) {
            $this->model->delRedis($id);
            $this->model->redis->zRem($this->model->redis_key . ':rank', $id);
            $this->model->addToRecycle($id, 'DistributorModel', '用户管理', $this->currentUser['user_id'], $this->currentUser['user_type'],'distributor');
            return self::returnMsg(1);
        } else {
            return self::returnMsg(1002);
        }
    }

    /**
     * 保存状态
     * @param Request $request
     * @return \think\response\Json
     */
    public function onStatus(Request $request)
    {
        $formData = [
            'id' => $request->param('id'),
            'status' => $request->param('status'),
        ];
        $validate = 'app\validate\DistributorValidate.upstatus';
        $result = $this->validate($formData, $validate);

        if (true !== $result) {
            return self::returnMsg(991, [], $result);
        } else {
            $ret = $this->model->saveData($formData, false, false);
            if (false !== $ret['flag']) {
                return self::returnMsg(1);
            } else {
                return self::returnMsg(0);
            }
        }
    }
}
